<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Email;
use think\Db;
use think\Qmail;
use think\Session;
use think\Controller\redirect;

class Login extends Controller
{
    /**
     * 想评论先登录
     *
     * @return \think\Response
     */
    public function login($id,$type)
    {

        return view('login/login',[
            'id'=>$id,
            'type'=>$type
        ]);
    }

    // 手机页面登录
    public function logindo(Request $request,$id,$type)
    {
        $req = $request->post();
        $tel = $req['tel'];
        $yzm = $req['yzm'];
        $yzm2 = $req['yzm2'];

        $data = Db::name('index_user')->where('tel', $tel)->find();
        Session::set('user_info', $data);
        // var_dump(Session::get('user_info'));die;

        $se = Session::get('user_info');

        if ($yzm == $yzm2 && $yzm != null && $yzm2 !== null) {
            if ($data > 0) {
                if(!empty($se)){
                    if($type == 'article'){
                        $this->redirect('article/index', ['id' => $id]);

                    }elseif($type == 'post'){
                        $this->redirect('post/detail', ['id' => $id]);
                    }

                }else{
                    $list = Db::name('article')->find($id);
                    return view('article/index',[
                        'list'=>$list
                    ]);
                }

            }
            else {
                echo "<script>alert('用户名或密码不正确!'); window.history.back(-1);</script>";
                die;
            }
        } else {
            echo "<script>alert('验证码错误!'); window.history.back(-1);</script>";
            die;
        }
    }

    // 容联云短信验证
    public function goggle(Request $request)
    {

        $req = $request->post('tel');
        $goggle = new Email();
        $yzm = $request->post('rand');
        setCookie('sms_goggle', $yzm, time() + 120, '/');
        $phone = $goggle->sendTemplateSMS($req, array($yzm, '2'));
        return view('index/login/login');
    }

    // qq登录页面
    public function email()
    {
        return view('login/email');
    }

    // 邮箱登录处理
    public function loginmail(Request $request)
    {
        $req = $request->post();
        $email = $req['email'];
        $yzm = $req['yzm'];
        $yzm2 = $req['yzm2'];
        $data = Db::name('index_user')->where('email', $email)->find();
        Session::set('user_info', $data);
        if ($yzm == $yzm2 && $yzm != null && $yzm2 !== null) {
            if ($data > 0) {
                $this->redirect('/');
            } else {
                echo "<script>alert('用户名或密码不正确!'); window.history.back(-1);</script>";
                die;
            }
        } else {
            echo "<script>alert('验证码错误!'); window.history.back(-1);</script>";
            die;
        }
    }
    // qq邮箱验证
    public function mail(Request $request)
    {

        $req = $request->post('email');

        $qmail = new Qmail();
        $yzm = $request->post('rand');
        setCookie('sms_mail', $yzm, time() + 120, '/');
        $qmail->sendMail("$req",'lz','您的验证码是', "$yzm");

        return view('login/email');
    }

    public function register()
    {
        return view('login/register');
    }

    public function registerdo(Request $request)
    {
        $req = $request->post();
        $data = [
            'username' => $req['username'],
            'email' => $req['email'],
            'tel' => $req['tel']
        ];
        $add = Db::name('index_user')->insert($data);
        if ($add < 0) {
            echo "<script>alert('注册失败');</script>";
            exit;
        } else {
            $this->redirect('login/login');
        }
    }

    public function qqLogin($code)
    {
        $data = forQQ($code);

        // 查询 token 是否存在于 user 表中
        $result = Db::name('index_user')->where(['token'=>$data['token']])->find();
        if(empty($result)){
            //插入数据
            $insert = [
                'username' => $data['nickname'],
                'token' => $data['token'],
                'regtime' => $data['regtime']
            ];
            // $res = Db::name('index_user')->insertGetId($data['token']);
            $res = Db::name('index_user')->insert($insert);
            if(!$res){
                $this->error('登录失败');
            }else{
                //存session  id
                // $data['id'] = $res;
                Session::set('user_info',$result);
                $this->success('登录成功','index/index/index');
            }
        }else{
            // 存Session
            Session::set('user_info',$result);
            $this->success('登录成功','index/index/index');
        }
    }
}
